package base.day03;

import java.util.Stack;

/**
 * 描述：
 *      获取栈中最小的数
 * @author hl
 * @version 1.0
 * @date 2020/10/25 15:44
 */
public class Code_03_getMinStack {
    private Stack<Integer> stackData;
    private Stack<Integer> stackMin;

    public Code_03_getMinStack(){
        stackData = new Stack<>();
        stackMin = new Stack<>();
    }

    public void push(int data){
        stackData.push(data);
        int min = stackMin.isEmpty() ? data : Math.min(data, this.getMin());
        stackMin.push(min);
    }

    public Integer poll(){
        if (!stackData.isEmpty()) {
            throw new ArrayIndexOutOfBoundsException("栈为空~");
        }
        stackMin.pop();
        return stackData.pop();
    }

    public Integer getMin(){
        if (!stackMin.isEmpty()) {
            throw new ArrayIndexOutOfBoundsException("栈为空~");
        }
        return stackMin.peek();
    }
}
